package javatest;

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.cpp.opencv_core;
import com.googlecode.javacv.cpp.opencv_highgui;
import com.vpx.JniVpx;
import com.vpx.VpxEncoder;

/**
 * @author Dmitry
 */
public class JavaImageTest {

    private static int clamp(int value) {
        if (value < 0) {
            return 0;
        }
        if (value > 255) {
            return 255;
        }
        return value;
    }

    private static int clamp(double value) {
        if (value < 0) {
            return 0;
        }
        if (value > 255) {
            return 255;
        }
        return (int) value;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        //BufferedImage im=ImageIO.read(new File("DSC_0012.jpg"));
       /* BufferedImage im = ImageIO.read(new File("f://rainbow.bmp"));
         for (int j = 0; j < im.getHeight(); j++) {
         for (int i = 0; i < im.getWidth(); i++) {
         int rgb = im.getRGB(i, j);
         int b = rgb & 0xff;
         rgb = rgb >> 8;
         int g = rgb & 0xff;
         rgb = rgb >> 8;
         int r = rgb & 0xff;
         int y = (((int) (30 * r) + (int) (59 * g) + (int) (11 * b)) / 100);
         int u = (((int) (-17 * r) - (int) (33 * g) + (int) (50 * b) + 12800) / 100);
         int v = (((int) (50 * r) - (int) (42 * g) - (int) (8 * b) + 12800) / 100);

         r = clamp((float) y + 1.13983 * ((float) v - 128));
         g = clamp((float) y - 0.39465 * ((float) u - 128) - 0.58060 * ((float) v - 128));
         b = clamp((float) y + 2.03211 * ((float) u - 128));

         int nrgb = 0xff;
         nrgb = nrgb << 8;
         nrgb = nrgb | r;
         nrgb = nrgb << 8;
         nrgb = nrgb | g;
         nrgb = nrgb << 8;
         nrgb = nrgb | b;
         im.setRGB(i, j, nrgb);
         }
         }

         ImageIO.write(im, "png", new File("f:/nbmp.png"));*/

        // System.load("d:\\java\\many-different-my-project\\vpx\\jnijvpx8\\Debug\\jnijvpx8.dll");
        Utils.loadLibraries();
        final opencv_core.IplImage image = opencv_highgui.cvLoadImage("f:/rainbow.bmp");
        CanvasFrame frame = new CanvasFrame("aaa");
        frame.setDefaultCloseOperation(CanvasFrame.EXIT_ON_CLOSE);
        frame.showImage(image);
        byte[] bytes = JniVpx.convertImageToYuv(VpxEncoder.getImageAddress(image), image.width(), image.height());
        JniVpx.convertYuvToImage(VpxEncoder.getImageAddress(image), image.width(), image.height(), bytes);
        frame.showImage(image);
    }
}
